package com.lulu.baselib.util;

import androidx.annotation.Nullable;

import com.google.gson.Gson;

import java.lang.reflect.Type;

/**
 * Author: XuChang
 * Date: 2019/10/23 16:34
 * Email: 123286473@qq.com
 * Description:
 * History:
 */
public class GsonUtil {

    @Nullable
    public static <T> T parse(String json, Class<T> classOfT) {
        T t = null;
        try {
            t = new Gson().fromJson(json, classOfT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> T parse(String json, Type typeOfT) {
        T t = null;
        try {
            t = new Gson().fromJson(json, typeOfT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static String toJson(Object object) {
        String json = "";
        try {
            json = new Gson().toJson(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}
